package com.huaishushu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaishushu.entity.Employee;
import com.huaishushu.mapper.EmployeeMapper;
import com.huaishushu.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author ChiBaoBaoHuaiShuShu
 * @since 2023-05-28
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    /**
     * 根据用户的username查询是否存在
     * @param username
     * @return
     */
    @Override
    public Employee login(String username) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, username);
        Employee employee = employeeMapper.selectOne(lqw);
        return employee;
    }

    /**
     * 新增员工信息
     * @param employee
     * @return
     */
    @Override
    public boolean save(HttpServletRequest request, Employee employee) {
        //设置初始密码并进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录用户的id
        //Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //setUpdateUser(empId);
        if (employeeMapper.insert(employee) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> page(Integer page, Integer pageSize, String name) {
        //构建分页构造器
        Page<Employee> pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNoneEmpty(name), Employee::getName, name);
        lqw.orderByDesc(Employee::getUpdateTime);
        //此处不返回是因为查询好后的值是直接赋值给了上面的分页构造器了pageInfo
        employeeMapper.selectPage(pageInfo, lqw);
        return pageInfo;
    }

    /**
     * 启用/禁用员工账号
     * @param employee
     * @return
     */
    @Override
    public boolean update(HttpServletRequest resource, Employee employee) {
        //通过session获取当前登录账号的id
        Long empId = (Long) resource.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);

        //根据id修改即可
        if (employeeMapper.updateById(employee) > 0) {
            return true;
        }
        return false;
    }

}
